package com.cqupt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqupt.exception.BusinessException;
import com.cqupt.mapper.WordMapper;
import com.cqupt.model.pojo.Word;
import com.cqupt.model.pojo.WordUnknown;
import com.cqupt.model.vo.ResultCodeEnum;
import com.cqupt.model.vo.WordVO;
import com.cqupt.service.WordUnknownService;
import com.cqupt.mapper.WordUnknownMapper;
import com.cqupt.utils.AuthContextUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class WordUnknownServiceImpl extends ServiceImpl<WordUnknownMapper, WordUnknown>
        implements WordUnknownService {

    @Resource
    private WordUnknownMapper wordUnknownMapper;

    @Resource
    private WordMapper wordMapper;

    @Override
    public void unknown(int wordId, int bookId) {
        Integer userId = AuthContextUtil.get().getId();
        LambdaQueryWrapper<WordUnknown> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WordUnknown::getWordId, wordId);
        wrapper.eq(WordUnknown::getBookId, bookId);
        wrapper.eq(WordUnknown::getUserId, userId);
        WordUnknown one = wordUnknownMapper.selectOne(wrapper);
        if (ObjectUtil.isNotNull(one)) {
            throw new BusinessException(ResultCodeEnum.DATA_ERROR);
        }
        WordUnknown wordUnknown = new WordUnknown();
        wordUnknown.setUserId(userId);
        wordUnknown.setWordId(wordId);
        wordUnknown.setBookId(bookId);
        wordUnknownMapper.insert(wordUnknown);
    }

    @Override
    public List<Word> getAllUnknown() {
        Integer userId = AuthContextUtil.get().getId();
        return wordUnknownMapper.getAllUnknown(userId);

    }

    @Override
    public List<Word> getLearn() {
        Integer userId = AuthContextUtil.get().getId();
        return wordUnknownMapper.getLearn(userId);

    }

    @Override
    public List<Word> getUnLearn() {
        Integer userId = AuthContextUtil.get().getId();
        return wordUnknownMapper.getUnLearn(userId);
    }

    @Override
    public List<Word> getIsKnown() {
        Integer userId = AuthContextUtil.get().getId();
        return wordUnknownMapper.getIsKnown(userId);
    }

    @Override
    public void mark(int wordId) {
        Integer userId = AuthContextUtil.get().getId();
        LambdaQueryWrapper<WordUnknown> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WordUnknown::getUserId, userId);
        wrapper.eq(WordUnknown::getWordId, wordId);
        WordUnknown one = wordUnknownMapper.selectOne(wrapper);
        if (ObjectUtil.isNull(one)) {
            throw new BusinessException(ResultCodeEnum.DATA_ERROR);
        }
        one.setIsLearn(1);
        one.setIsKnown(1);
        wordUnknownMapper.updateById(one);
    }

    @Override
    public void unmark(int wordId) {
        Integer userId = AuthContextUtil.get().getId();
        LambdaQueryWrapper<WordUnknown> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WordUnknown::getUserId, userId);
        wrapper.eq(WordUnknown::getWordId, wordId);
        WordUnknown one = wordUnknownMapper.selectOne(wrapper);
        one.setIsLearn(0);
        one.setIsKnown(0);
        wordUnknownMapper.updateById(one);
    }

    @Override
    public void cancel(int wordId, int bookId) {
        Integer userId = AuthContextUtil.get().getId();
        LambdaQueryWrapper<WordUnknown> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WordUnknown::getUserId, userId);
        wrapper.eq(WordUnknown::getWordId, wordId);
        wrapper.eq(WordUnknown::getBookId, bookId);
        WordUnknown one = wordUnknownMapper.selectOne(wrapper);
        if (ObjectUtil.isNotNull(one)) {
            wordUnknownMapper.deleteById(one);
        } else {
            throw new BusinessException(ResultCodeEnum.DATA_ERROR);
        }
    }

    @Override
    public WordVO ismark(Integer wordId) {
        Integer userId = AuthContextUtil.get().getId();
        LambdaQueryWrapper<WordUnknown> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WordUnknown::getWordId, wordId);
        wrapper.eq(WordUnknown::getUserId, userId);
        WordUnknown one = wordUnknownMapper.selectOne(wrapper);
        Word word = wordMapper.selectById(wordId);
        WordVO wordVO = new WordVO();
        BeanUtil.copyProperties(word, wordVO);
        if (ObjectUtil.isNull(one)) {
            return wordVO;
        } else {
            wordVO.setIsmark(1);
            return wordVO;
        }
    }
}




